package com.clw.controller;

import com.clw.dataobject.SupplierInfo;
import com.clw.enums.ResultEnum;
import com.clw.exception.SellException;
import com.clw.form.SupplierInfoForm;
import com.clw.service.SupplierInfoService;
import com.clw.utils.NumberUtils;
import com.clw.utils.ResultVOUtils;
import com.clw.utils.StringUtils;
import com.clw.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商
 * 2017-07-30 15:28
 */
@Controller
@RequestMapping("/supplier_info")
public class SupplierInfoController {

    @Autowired
    private SupplierInfoService supplierInfoService;

    @RequestMapping("/list")
    public ModelAndView list(){
        return new ModelAndView("supplier_info/index");
    }
    @ResponseBody
    @RequestMapping("/getData")
    public Map<String,Object> getData(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "rows", defaultValue = "10") Integer rows,
                                      HttpServletRequest request) {
        PageRequest request1=new PageRequest(page-1,rows);
        String name=request.getParameter("name");
        String phone=request.getParameter("phone");
        String status=request.getParameter("status");
        Page<SupplierInfo> sellerInfos=supplierInfoService.findAll(name,phone,status,request1);
        Map<String,Object> map=new HashMap<>();
        map.put("rows",sellerInfos.getContent());
        map.put("total",supplierInfoService.getSize(name,phone,status));
        return map;
    }

    @RequestMapping("/view")
    public ModelAndView addView(@RequestParam(value = "id",required = false) Integer id,
                                Map<String,Object> map){
        SupplierInfo supplierInfo;
        if (StringUtils.isNotEmpty(id)){
            supplierInfo=supplierInfoService.findOne(id);
        }
        else {
            supplierInfo=new SupplierInfo();
            supplierInfo.setSupNo(NumberUtils.getMiRandom("sup"));
        }
        map.put("supplierInfo",supplierInfo);
        return new ModelAndView("supplier_info/add_view");
    }


    @ResponseBody
    @PostMapping("del")
    public ResultVO del(@RequestParam(value = "id",required = false) Integer id){
        SupplierInfo supplierInfo=supplierInfoService.findOne(id);
        if (supplierInfo==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        supplierInfoService.del(supplierInfo);
        return ResultVOUtils.success("删除成功");
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultVO save(@Valid SupplierInfoForm form, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOUtils.error(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        SupplierInfo supplierInfo=new SupplierInfo();
        try{
            if (StringUtils.isNotEmpty(form.getId())){
                supplierInfo=supplierInfoService.findOne(form.getId());
            }
            else {
                form.setStatus(0);
            }
            BeanUtils.copyProperties(form,supplierInfo);
            supplierInfoService.save(supplierInfo);
        }catch (SellException e){
            return ResultVOUtils.error(ResultEnum.SAVE_ERROR.getCode(),
                    ResultEnum.SAVE_ERROR.getMessage());
        }
        return ResultVOUtils.success("保存成功");
    }

    @ResponseBody
    @PostMapping("/review")
    public ResultVO review(@RequestParam(value = "id",required = false) Integer id,
                           @RequestParam(value = "review",required = false) Integer review){
        SupplierInfo supplierInfo=supplierInfoService.findOne(id);
        if (supplierInfo==null){
            return ResultVOUtils.error(ResultEnum.NO_FOUND.getCode(),
                    ResultEnum.NO_FOUND.getMessage());
        }
        supplierInfo.setStatus(review);
        supplierInfoService.save(supplierInfo);
        return ResultVOUtils.success("操作成功");
    }

    @ResponseBody
    @RequestMapping("/getAllValid")
    public List<SupplierInfo> getAllValid(){
        return supplierInfoService.findValid(1);
    }
}
